package pers.qianyu.month_202011.date_20201116;

/**
 * 121. 买卖股票的最佳时机
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
 *
 * @author mizzle rain
 * @date 2020-11-16 16:05
 */
public class MaxProfit {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int max = 0;
        int min = prices[0];
        for (int i = 0; i < prices.length; i++) {
            int p = prices[i];
            min = Math.min(p, min);
            max = Math.max(max, p - min);
        }
        return max;
    }

    public static void main(String[] args) {
        int[] prices = {7, 1, 5, 3, 6, 4};
        int result = new MaxProfit().maxProfit(prices);
        System.out.println("result = " + result);
    }
}
